# apps/rooms/model.py

from django.db import models
from django.db.models.signals import post_migrate
from django.dispatch import receiver


class Room(models.Model):
    """
    房间表：统一管理‘客厅 / 卧室 / 厨房 …’等实体。
    """
    room_id    = models.AutoField(primary_key=True)
    name       = models.CharField(max_length=50, unique=True)
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = "rooms"

    def __str__(self):
        return self.name


# ---------- 初始化房间 ----------
@receiver(post_migrate)
def create_initial_rooms(sender, **kwargs):
    """
    在迁移后自动创建默认房间。
    """
    if sender.name != "apps.rooms":
        return

    default_rooms = ["客厅", "厨房", "客房", "卧室", "卫生间", "阳台"]
    for room_name in default_rooms:
        obj, created = Room.objects.get_or_create(name=room_name)
        if created:
            print(f"[rooms] Created room: {obj.name} (id={obj.room_id})")